package com.mkk.library.utils

import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.select.Elements

object HtmlUtils {
    /**
     * 富文本适配
     */
    fun getHtmlData(bodyHTML: String?): String? {
        return "<html>" +
                "<header>" +
                "<meta charset=\"utf-8\" name=\"viewport\" content=\"width=device-width,initial-scale=1.0, minimum-scale=1.0,initial-scale=1.0, user-scalable=no\">" +
                "<style type=\"text/css\">" +
                "img{max-width:100%;width:100%;height:auto;}" +
                "body{width:100%;margin-right:15px;margin-left:15px;margin-top:15px;font-size:45px;}" +
                "</style>" +
                "</header>" +
                "<body>" +
                getNewContent(bodyHTML!!) +
                "</body>" +
                "</html>"
    }

    // 高版本webView屏幕适配
    private fun getNewContent(htmlText: String): String? {
        val doc: Document? = Jsoup.parse(htmlText)
        val elementsImg: Elements = doc!!.getElementsByTag("img")
        for (element in elementsImg) {
            element.attr("width", "100%").attr("height", "auto")
        }
        val elementsP: Elements = doc.getElementsByTag("p")
        for (element in elementsP) {
            element.attr("width", "100%").attr("height", "auto")
        }
        return doc.toString()
    }
}